package cn.edu.cqu.services.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.cqu.domains.PartyNews;
import cn.edu.cqu.services.PartyNewsCRUD;

@Service("PartyNewsCRUDHibernate")
public class PartyNewsCRUDHibernate implements PartyNewsCRUD {

	private HibernateTemplate ht;
	private Session session;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		ht = new HibernateTemplate(sessionFactory);
		session = ht.getSessionFactory().openSession();
	}
	
	@Override
	public PartyNews getByID(String pID) {
		return ht.get(PartyNews.class, pID);
	}

	@Override
	@Transactional
	public PartyNews edit(PartyNews partyNews) {
		return ht.merge(partyNews);
	}

	@Override
	@Transactional
	public void deleteByID(String pID) {
		PartyNews partyNews = this.getByID(pID);
		ht.delete(partyNews);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PartyNews> getAll(int pageNo, int pageSize) {
		String hql = "from PartyNews  order by pNewsDate desc";
		Query query = session.createQuery(hql);
		return query.setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
	}

//	@SuppressWarnings("unchecked")
//	@Override
//	public List<PartyNews> getSubAll(int subType,int pageNo, int pageSize) {
//		String hql = "from PartyNews where pNewsType=?";
//		Query query = session.createQuery(hql);
//		query.setInteger(0, subType);
//		return query.setFirstResult((pageNo-1)*pageSize).setMaxResults(pageSize).list();
//	}

}
